package activity.messge;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.hutoubenjg.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import base.BaseActivity;
import base.MyApplcation;
import bean.messge_bean.SystemMessgeBean;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;
import presenter.SysTemPresenter;
import util.utls.BasePtr;
import util.utls.ByAlert;
import util.utls.IsUtilUid;
import util.utls.TimeFormat;
import util.volley.ResultEntity;

/**
 * Created by itboye on 2016/12/10.
 */

public class RepairMessActivity extends BaseActivity implements Observer {

    private LinearLayout linSystemMessge;
    private ListView messgeListview;
    SysTemPresenter presenter;
    private List<SystemMessgeBean.ListEntity> list;
    MessageAdapter adapter;
    private TextView tvTitle, tvTest;
    private ImageView back;
    PtrFrameLayout commend_anchor_ptr;
    int pageIndex;
    LinearLayout kong_layoout;
    TextView kong_text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_messge);
        tvTitle.setText("消息列表");
        BasePtr.setPagedPtrStyle(commend_anchor_ptr);//设置上拉下拉刷新
        list=new ArrayList<SystemMessgeBean.ListEntity>();

        commend_anchor_ptr.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                //  shopMallSearchBean.setKeyword("");
                pageIndex++;
                presenter.onSysmTemMeesgeRes(IsUtilUid.isUid(),"6180",pageIndex+"","10");
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                presenter.onSysmTemMeesgeRes(IsUtilUid.isUid(),"6180",pageIndex+"","10");
            }
        });
        presenter=new SysTemPresenter(this);
        showProgressDialog("加载中,请稍后", true);
        presenter.onSysmTemMeesgeRes(IsUtilUid.isUid(),"6180",pageIndex+"","10");

    }

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
        }

    }

    @Override
    public void update(Observable observable, Object data) {

        ResultEntity entity = handlerError(data);
        if (entity != null) {
            IsUtilUid.tianjia(entity);
            if (entity.getEventType() == presenter.MessgeRE_success) {
                if(pageIndex==1){
                    list.clear();
                }
                commend_anchor_ptr.refreshComplete();//设置刷新完成
                SystemMessgeBean bean= (SystemMessgeBean) entity.getData();
                list.addAll(bean.getList());

                if (list.size() > 0) {
                    messgeListview.setVisibility(View.VISIBLE);
                    kong_layoout.setVisibility(View.GONE);
                    adapter=new MessageAdapter(MyApplcation.ctx,list);
                    messgeListview.setAdapter(adapter);
                } else {
                    if(pageIndex==1){
                        messgeListview.setVisibility(View.GONE);
                        commend_anchor_ptr.setVisibility(View.GONE);
                        kong_layoout.setVisibility(View.VISIBLE);
                        kong_text.setText("消息为空");
                    }
                }
            }
            if (entity.getEventType() == presenter.MessgeRE_fail) {
                ByAlert.alert(entity.getData());
            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
    }

    public class MessageAdapter extends BaseAdapter {
        Context context;
        List<SystemMessgeBean.ListEntity> been;

        public MessageAdapter(Context context, List<SystemMessgeBean.ListEntity> been) {
            this.context = context;
            this.been = been;
        }

        @Override
        public int getCount() {
            return been.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHoudr houdr;
            if (convertView == null) {
                houdr = new ViewHoudr();
                convertView = LayoutInflater.from(MyApplcation.ctx).inflate(R.layout.item_messge, null);
                houdr.MessgeName = (TextView) convertView.findViewById(R.id.tvmeesgeTitlte);
                houdr.MessgeTitle = (TextView) convertView.findViewById(R.id.remessge);
                houdr.tvMessgeTime = (TextView) convertView.findViewById(R.id.tvTimeS);
                convertView.setTag(houdr);
            } else {
                houdr = (ViewHoudr) convertView.getTag();
            }
            houdr.MessgeName.setText(been.get(position).getTitle());
            houdr.MessgeTitle.setText(been.get(position).getSummary());
            houdr.tvMessgeTime.setText(TimeFormat.DateFormat(been.get(position).getCreate_time()));

            return convertView;
        }

        private class ViewHoudr {
            private TextView MessgeTitle;
            private TextView MessgeName;
            private TextView tvMessgeTime;
        }
    }
}
